﻿namespace LMT
{
    using System;
    using System.Runtime.InteropServices;

    /// <summary>
    /// class wraper for win32 API
    /// </summary>
    internal abstract class Win32
    {
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct TokPriv1Luid
        {
            public int Count;
            public long Luid;
            public int Attr;
        }

        /// <summary>
        /// Gets the current process.
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll", ExactSpelling = true)]
        internal static extern IntPtr GetCurrentProcess();

        /// <summary>
        /// Opens the process token.
        /// </summary>
        /// <param name="h">The h.</param>
        /// <param name="acc">The acc.</param>
        /// <param name="phtok">The phtok.</param>
        /// <returns></returns>
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

        /// <summary>
        /// Lookups the privilege value.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="name">The name.</param>
        /// <param name="pluid">The pluid.</param>
        /// <returns></returns>
        [DllImport("advapi32.dll", SetLastError = true)]
        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

        /// <summary>
        /// Adjusts the token privileges.
        /// </summary>
        /// <param name="htok">The htok.</param>
        /// <param name="disall">if set to <c>true</c> [disall].</param>
        /// <param name="newst">The newst.</param>
        /// <param name="len">The len.</param>
        /// <param name="prev">The prev.</param>
        /// <param name="relen">The relen.</param>
        /// <returns></returns>
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
            ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

        /// <summary>
        /// Exits the windows ex.
        /// </summary>
        /// <param name="flg">The FLG.</param>
        /// <param name="rea">The rea.</param>
        /// <returns></returns>
        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool ExitWindowsEx(int flg, int rea);

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="hWnd">The h WND.</param>
        /// <param name="msg">The MSG.</param>
        /// <param name="wParam">The w param.</param>
        /// <param name="lParam">The l param.</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        internal static extern UInt32 SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);

        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";


        /// <summary>
        /// Locks the work station.
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll")]
        internal static extern bool LockWorkStation();

        /// <summary>
        /// variable send as system command
        /// </summary>
        internal const int WM_SYSCOMMAND = 0x0112;

        /// <summary>
        /// the message is sent to all top-level windows in the system
        /// </summary>
        internal const int HWND_BROADCAST = 0xffff;
        
        /// <summary>
        /// the message is sent to one top-level window in the system
        /// </summary>
        internal const int HWND_TOPMOST = -1;

        /// <summary>
        /// all option for Turn off Environment
        /// </summary>
        internal enum MachineTurnOff : int
        {
            LogOff = 0x00000000,
            Shutdown = 0x00000001,
            Reboot = 0x00000002,
            Force = 0x00000004,
            PowerOff = 0x00000008,
            ForceIfHung = 0x00000010
        }

        /// <summary>
        /// Environment Privileges
        /// </summary>
        internal enum Privileges : int 
        {
            PrivilegEnable = 0x00000002,
            Querry = 0x00000008,
            AdjustPrivilege = 0x00000020,
        }
    }
}
